package org.tmad.ocms.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tmad.ocms.util.CommonUtils;

/**
 * Servlet implementation class ForgotPasswordServlet
 */
public class Forgotpwd extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Forgotpwd() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// username
		// activationCode
		@SuppressWarnings("unused")
		String email = request.getParameter("email");
		@SuppressWarnings("unused")
		String activationCode = request.getParameter("activationCode");
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/active.jsp");
		dispatcher.forward(request, response);


	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		@SuppressWarnings("unused")
		String password = request.getParameter("password");
		
		
		@SuppressWarnings("unused")
		String confirmpassword = request.getParameter("confirmpassword");
		String email = request.getParameter("email");
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/ocms", "root", "root");

			PreparedStatement ps = con
					.prepareStatement("update volunteer set password=? where email_Id=?");
			ps.setString(1, CommonUtils.getEncryptedPassword(request.getParameter("password")));
			ps.setString(2, email);

			int i = ps.executeUpdate();
			if (i == 1) {
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("error", "Your password has been reset successfuly.\n Please login with your new credentials");
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/login.jsp");
		dispatcher.forward(request, response);

	}

}
